package com.heima.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.heima.pojo.TbRegion;
import com.heima.mapper.TbRegionMapper;
import com.heima.pojo.dto.TbRegionDto;
import com.heima.pojo.vo.PageResponse;
import com.heima.pojo.vo.TbRegionVo;
import com.heima.service.ITbRegionService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 运营区域 服务实现类
 * </p>
 *
 * @author AkiRinko
 * @since 2024-06-07
 */
@Service
@RequiredArgsConstructor
public class TbRegionServiceImpl extends ServiceImpl<TbRegionMapper, TbRegion> implements ITbRegionService {
    private final TbRegionMapper tbRegionMapper;

    /**
     * 区域搜索
     *
     * @param pageIndex
     * @param pageSize
     * @param name
     * @return
     */
    @Override
    public PageResponse<TbRegionVo> selectRegionPage(Integer pageIndex, Integer pageSize, String name) {
        Page<TbRegionVo> page = new Page<>(pageIndex, pageSize);

        Page<TbRegionVo> result = tbRegionMapper.selectRegionPage(name, page);

        return PageResponse.of(result, TbRegionVo.class);
    }

    /**
     * 新增区域
     *
     * @param dto
     */
    @Override
    public void insertRegion(TbRegionDto dto) {
        TbRegion tbRegion = BeanUtil.copyProperties(dto, TbRegion.class);
        tbRegion.setName(dto.getRegionName());
        tbRegionMapper.insert(tbRegion);
    }

    /**
     * 修改区域
     *
     * @param regionId
     * @param dto
     */
    @Override
    public void updateRegion(String regionId, TbRegionDto dto) {
        TbRegion tbRegion = BeanUtil.copyProperties(dto, TbRegion.class);
        tbRegion.setName(dto.getRegionName());

        LambdaQueryWrapper<TbRegion> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(TbRegion::getId, regionId);

        tbRegionMapper.update(tbRegion, wrapper);
    }

    /**
     * 删除区域
     *
     * @param regionId
     */
    @Override
    public void deleteRegion(String regionId) {
        tbRegionMapper.deleteById(regionId);
    }

}
